package cn.wen.javax.servlet.jsp.tagext.form;

import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;

import cn.wen.javax.servlet.jsp.tagext.AbstractTag;
import cn.wen.javax.servlet.jsp.tagext.builder.form.*;

public class InputTag extends AbstractTag {

    private static final long serialVersionUID = -3342224544064575208L;

    @Override
    public int doStartTag() throws JspException {
	if (this.type.toLowerCase().equals("text")) {
	    builder = new InputTextBuilder(this);
	}
	else if (this.type.toLowerCase().equals("radio")) {
	    builder = new InputRadioBuilder(this);
	}
	else if (this.type.toLowerCase().equals("checkbox")) {
	    builder = new InputCheckBoxBuilder(this);
	}
	else if (this.type.toLowerCase().equals("hidden")) {
	    builder = new InputHiddenBuilder(this);
	}
	else if (this.type.toLowerCase().equals("password")) {
	    builder = new InputPasswordBuilder(this);
	}
	else if (this.type.toLowerCase().equals("file")) {
	    builder = new InputFileBuilder(this);
	}
	else if (this.type.toLowerCase().equals("button")) {
	    builder = new InputButtonBuilder(this);
	}
	else if (this.type.toLowerCase().equals("submit")) {
	    builder = new InputSubmitBuilder(this);
	}
	else if (this.type.toLowerCase().equals("reset")) {
	    builder = new InputResetBuilder(this);
	}
	return super.doStartTag();
    }

    @Override
    public int doEndTag() throws JspException {
	builder.process();
	builder.render(this.pageContext.getOut());
	return super.doEndTag();
    }

    // ---------------------------------------------------------------------
    // this tag Attributes
    // ---------------------------------------------------------------------

    private String type;

    private Object value;

    private List<Map<String, ?>> options;

    public void setType(String type) {
	this.type = type;
    }

    public Object getValue() {
	return value;
    }

    public void setValue(Object value) {
	if (value == null) return;
	this.value = this.valueStack.findValue(value.toString());
    }

    public List<Map<String, ?>> getOptionList() {
	return options;
    }

    public void setOptions(Object options) {
	if (options == null) return;
	this.options = (List) this.valueStack.findValue(options.toString());
    }

}
